package com.zenithmind.document.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zenithmind.common.context.UserContext;
import com.zenithmind.common.result.Result;
import com.zenithmind.document.pojo.dto.DocumentCreateDTO;
import com.zenithmind.document.pojo.dto.DocumentUpdateDTO;
import com.zenithmind.document.pojo.dto.DocumentUploadDTO;
import com.zenithmind.document.pojo.query.DocumentQueryDTO;
import com.zenithmind.document.pojo.vo.DocumentDetailVO;
import com.zenithmind.document.pojo.vo.DocumentVO;
import com.zenithmind.document.service.DocumentBasicService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import java.util.List;

/**
 * 文档基础管理控制器
 * 遵循单一职责原则：专注于文档基础CRUD操作
 * 遵循依赖倒置原则：依赖抽象服务接口
 * 遵循接口隔离原则：只提供基础管理功能
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Slf4j
@RestController
@RequestMapping("/api/document/basic")
@RequiredArgsConstructor
@Validated
@Tag(name = "文档基础管理", description = "文档基础CRUD操作接口")
public class DocumentBasicController {

    private final DocumentBasicService documentBasicService;

    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @Operation(summary = "上传文档", description = "上传文档文件并创建文档记录")
    public Result<DocumentVO> uploadDocument(@Valid DocumentUploadDTO uploadDTO) {
        log.info("上传文档请求: {}", uploadDTO.getFile().getOriginalFilename());
        
        DocumentVO document = documentBasicService.uploadDocument(uploadDTO);
        
        return Result.success(document);
    }

    @PostMapping
    @Operation(summary = "创建文档", description = "创建新的文档记录")
    public Result<DocumentVO> createDocument(@Valid @RequestBody DocumentCreateDTO createDTO) {
        log.info("创建文档请求: {}", createDTO.getName());
        
        DocumentVO document = documentBasicService.createDocument(createDTO);
        
        return Result.success(document);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新文档", description = "更新指定文档的信息")
    public Result<Boolean> updateDocument(
            @Parameter(description = "文档ID", required = true) @PathVariable @NotBlank String id,
            @Valid @RequestBody DocumentUpdateDTO updateDTO) {
        
        log.info("更新文档请求: id={}", id);
        
        Boolean result = documentBasicService.updateDocument(id, updateDTO);
        
        return Result.success(result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除文档", description = "删除指定的文档")
    public Result<Boolean> deleteDocument(
            @Parameter(description = "文档ID", required = true) @PathVariable @NotBlank String id) {
        
        log.info("删除文档请求: id={}", id);
        
        Boolean result = documentBasicService.deleteDocument(id);
        
        return Result.success(result);
    }

    @DeleteMapping("/batch")
    @Operation(summary = "批量删除文档", description = "批量删除多个文档")
    public Result<Boolean> batchDeleteDocuments(
            @Parameter(description = "文档ID列表", required = true) @RequestBody @NotEmpty List<String> ids) {
        
        log.info("批量删除文档请求: ids={}", ids);
        
        Boolean result = documentBasicService.batchDeleteDocuments(ids);
        
        return Result.success(result);
    }

    @PutMapping("/{id}/restore")
    @Operation(summary = "恢复文档", description = "恢复已删除的文档")
    public Result<Boolean> restoreDocument(
            @Parameter(description = "文档ID", required = true) @PathVariable @NotBlank String id) {
        
        log.info("恢复文档请求: id={}", id);
        
        Boolean result = documentBasicService.restoreDocument(id);
        
        return Result.success(result);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询文档", description = "分页查询文档列表")
    public Result<IPage<DocumentVO>> getDocumentPage(@Valid DocumentQueryDTO queryDTO) {
        log.info("分页查询文档请求");
        
        IPage<DocumentVO> result = documentBasicService.getDocumentPage(queryDTO);
        
        return Result.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取文档详情", description = "获取指定文档的详细信息")
    public Result<DocumentDetailVO> getDocumentDetail(
            @Parameter(description = "文档ID", required = true) @PathVariable @NotBlank String id) {
        
        log.info("获取文档详情请求: id={}", id);
        
        String userId = UserContext.getUserId();
        DocumentDetailVO result = documentBasicService.getDocumentDetail(id, userId);
        
        return Result.success(result);
    }

    @GetMapping("/{id}/info")
    @Operation(summary = "获取文档基本信息", description = "获取指定文档的基本信息")
    public Result<DocumentVO> getDocumentById(
            @Parameter(description = "文档ID", required = true) @PathVariable @NotBlank String id) {
        
        log.info("获取文档基本信息请求: id={}", id);
        
        DocumentVO result = documentBasicService.getDocumentById(id);
        
        return Result.success(result);
    }

    @GetMapping("/{id}/exists")
    @Operation(summary = "检查文档是否存在", description = "检查指定文档是否存在")
    public Result<Boolean> existsDocument(
            @Parameter(description = "文档ID", required = true) @PathVariable @NotBlank String id) {
        
        log.info("检查文档存在请求: id={}", id);
        
        Boolean result = documentBasicService.existsDocument(id);
        
        return Result.success(result);
    }

    @GetMapping("/{id}/permission")
    @Operation(summary = "检查文档权限", description = "检查当前用户对指定文档的权限")
    public Result<Boolean> checkDocumentPermission(
            @Parameter(description = "文档ID", required = true) @PathVariable @NotBlank String id,
            @Parameter(description = "权限类型", required = true) @RequestParam @NotBlank String permission) {
        
        log.info("检查文档权限请求: id={}, permission={}", id, permission);
        
        String userId = UserContext.getUserId();
        Boolean result = documentBasicService.checkDocumentPermission(id, userId, permission);
        
        return Result.success(result);
    }

    @GetMapping("/{id}/ownership")
    @Operation(summary = "验证文档所有权", description = "验证当前用户是否为指定文档的所有者")
    public Result<Boolean> validateDocumentOwnership(
            @Parameter(description = "文档ID", required = true) @PathVariable @NotBlank String id) {
        
        log.info("验证文档所有权请求: id={}", id);
        
        String userId = UserContext.getUserId();
        Boolean result = documentBasicService.validateDocumentOwnership(id, userId);
        
        return Result.success(result);
    }

    @GetMapping("/health")
    @Operation(summary = "健康检查", description = "检查文档基础服务的健康状态")
    public Result<String> healthCheck() {
        log.info("文档基础服务健康检查");
        
        return Result.success("文档基础服务运行正常");
    }
}
